package ru.sh.screen;

import javax.microedition.khronos.opengles.GL10;

import ru.sh.bg.SimpleBg;
import ru.sh.bullet.SimpleBullet;
import ru.sh.factory.HeartFactory;
import ru.sh.factory.JewelFactory;
import ru.sh.heart.SimpleHeart;
import ru.sh.hero.Soldier;
import ru.sh.jewel.Jewel;
import ru.sh.panel.HealthPanel;
import ru.sh.ui.SimpleUI;
import android.hardware.SensorEvent;
import android.view.MotionEvent;
import framework.Game;
import framework.GameScreen;
import framework.objects.DynamicGameObject;
import framework.objects.DynamicGameObjectsGroup;
import framework.objects.Getable;
import framework.objects.Hitable;
import framework.objects.ObjectFactory;
import framework.objects.bullets.Bullet;
import framework.panel.Panel;

public class SHGameScreen extends GameScreen {

	private DynamicGameObjectsGroup<SimpleHeart> hearts;
	
	private DynamicGameObjectsGroup<SimpleBullet> bullets;
	
	private DynamicGameObjectsGroup<Jewel> jewels;
	
	private ObjectFactory<SimpleHeart> heartFactory;
	
	private ObjectFactory<Jewel> jewelFactory;
	
	private Panel healthPanel;
	
	private Panel scorePanel;
	
	private DynamicGameObject dynamicCloud;

	private SimpleBg bg;

	private Soldier hero;

	private SimpleUI ui;
	
	public Soldier getHero() {
		return hero;
	}
	
	public SHGameScreen(Game game) {
		super(game);	
	}

	public DynamicGameObjectsGroup<SimpleHeart> getHearts() {
		return hearts;
	}

	public DynamicGameObjectsGroup<SimpleBullet> getBullets() {
		return bullets;
	}
	
	public DynamicGameObjectsGroup<Jewel> getJewels() {
		return jewels;
	}

	public HealthPanel getHealthPanel() {
		return (HealthPanel)healthPanel;
	}
	
	@Override
	public void present(GL10 gl, float deltaTime) {
		bg.draw(gl, deltaTime);
		ui.draw(gl, deltaTime);
		hero.draw(gl, deltaTime);
		bullets.draw(gl, deltaTime);
		hearts.draw(gl, deltaTime);
		jewels.draw(gl, deltaTime);
//		healthPanel.drasw(gl);
//		dynamicCloud.draw(gl);
//		scorePanel.draw(gl);
		
		//TODO
		switch (currentState) {
			case PAUSE :
				break;
			case GAME_OVER :
				break;
		}
	}

	@Override
	public void update(float deltaTime) {
		switch (currentState) {
		case PLAY :
			hero.update(deltaTime);
			bullets.update(deltaTime);
			hearts.update(deltaTime);
			jewels.update(deltaTime);
//			dynamicCloud.update(deltaTime);
			createNewObjects(deltaTime);
			testCollision();
			break;
		case PAUSE :
			break;
		case GAME_OVER :
			break;
		}
	}

	private void testCollision() {
		for (Object bullet : bullets.getArray()) {
			if (bullet == null) continue;
			for (Object heart : hearts.getArray()) {
				if (heart == null) continue;
				((Bullet)bullet).tryToHit((Hitable)heart);
			}			
		}
		for (Object jewel : jewels.getArray()) {
			if (jewel == null) continue;
			hero.tryToGet((Getable)jewel);
		}
	}

	private void createNewObjects(float deltaTime) {
		heartFactory.createObject(deltaTime);
		jewelFactory.createObject(deltaTime);
	}

	@Override
	public void handleTouchEvent(MotionEvent event) {	
		switch (currentState) {
			case PLAY :
				ui.handle(event);
				break;
			case PAUSE :
				currentState = State.PLAY;
				break;
			case GAME_OVER :
				break;
		}
	}

	@Override
	protected void initScreenObjects() {
		hearts = new DynamicGameObjectsGroup<SimpleHeart>(20);
		bullets = new DynamicGameObjectsGroup<SimpleBullet>(5);
		jewels = new DynamicGameObjectsGroup<Jewel>(20);
		bg = new SimpleBg();
		hero = new Soldier(this);
		ui = new SimpleUI(this);		
		heartFactory = new HeartFactory(hearts, 7000);
		jewelFactory = new JewelFactory(jewels, 7000, hero);
//		healthPanel = new HealthPanel(hero);
//		dynamicCloud = new DynamicCloud();
//		scorePanel = new ScorePanel();
	}

	@Override
	public void handleAccelerometerEvent(SensorEvent event) {
		// TODO Auto-generated method stub
		
	}

}
